<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>留言板</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
  <style>
    .pagination {
      margin-top: 10px;
    }
    .pagination button {
      margin-right: 5px;
    }
  </style>
</head>
<body>
  <div id="app">
    <div>
      <input v-model="newMessage" type="text" placeholder="请输入留言内容">
      <button @click="addMessage">添加</button>
    </div>
    <ul>
      <li v-for="(message, index) in displayedMessages" :key="message.id">
        <div v-if="!message.editing">
          {{ message.text }}
        </div>
        <div v-else>
          <input v-model="message.editText">
          <button @click="saveMessage(message)">保存</button>
        </div>
        <button @click="editMessage(message)">编辑</button>
        <button @click="deleteMessage(index)">删除</button>
      </li>
    </ul>
    <div class="pagination">
      <button v-for="page in pageCount" :key="page" @click="changePage(page)">{{ page }}</button>
    </div>
  </div>

  <script>
    new Vue({
      el: '#app',
      data: {
        messages: [],
        newMessage: '',
        pageSize: 5,
        currentPage: 1
      },
      computed: {
        displayedMessages() {
          const start = (this.currentPage - 1) * this.pageSize;
          return this.messages.slice(start, start + this.pageSize);
        },
        pageCount() {
          return Math.ceil(this.messages.length / this.pageSize);
        }
      },
      methods: {
        addMessage() {
          const trimmedMessage = this.newMessage.trim();
          if (trimmedMessage) {
            this.messages.push({ id: Date.now(), text: trimmedMessage, editText: trimmedMessage, editing: false });
            this.newMessage = '';
          }
        },
        editMessage(message) {
          message.editing = true;
          message.editText = message.text;
        },
        saveMessage(message) {
          message.text = message.editText;
          message.editing = false;
        },
        deleteMessage(index) {
          const start = (this.currentPage - 1) * this.pageSize + index;
          this.messages.splice(start, 1);
        },
        changePage(page) {
          this.currentPage = page;
        }
      }
    });
  </script>
</body>
</html>
